package cn.edu.nuc.zyb.client;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.List;

import cn.edu.nuc.zyb.model.Goods;

/**
 * 此类负责管理员客户端
 * 
 * @author 张彦博
 *
 */
public class AdminClient {

	Socket socket;

	/**
	 * 此类的构造方法负责与服务器连接
	 */
	public AdminClient() {
		try {
			socket = new Socket("127.0.0.1", 4000);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 从服务器接收消息
	 */
	public boolean acceptMsg() {
		InputStream is = null;
		DataInputStream dis = null;
		try {
			is = socket.getInputStream();
			dis = new DataInputStream(is);
			return dis.readBoolean();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return false;

	}

	/**
	 * 向服务器发送展示列表的消息
	 */
	public void listGoods() {
		String msg = "adminlistgoods,";
		OutputStream os = null;
		DataOutputStream dos = null;
		try {
			os = socket.getOutputStream();
			dos = new DataOutputStream(os);
			dos.writeUTF(msg);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 发送通过id查询货物的消息
	 * 
	 * @param gid商品id
	 */
	public void searchGoods(int gid) {
		String msg = "adminsearchgoods," + gid;
		OutputStream os = null;
		DataOutputStream dos = null;
		try {
			os = socket.getOutputStream();
			dos = new DataOutputStream(os);
			dos.writeUTF(msg);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 接收从服务器返回来的goods对象
	 * 
	 * @return Goods对象
	 */
	public Goods acceptSearchGoods() {
		try {
			ObjectInputStream oos = new ObjectInputStream(socket.getInputStream());
			@SuppressWarnings("unchecked")
			List<Goods> list = (List<Goods>) oos.readObject();
			System.out.println(list.size());
			return list.get(0);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 向服务器发送添加货物的消息
	 * 
	 * @param goodsName商品名称
	 * @param价格
	 * @param 商品数量
	 * @param用户名
	 */
	public void addGoods(String goodsName, float price, int num, String intro) {
		String msg = "addgoods," + goodsName + "," + price + "," + num + "," + intro;
		OutputStream os = null;
		DataOutputStream dos = null;
		try {
			os = socket.getOutputStream();
			dos = new DataOutputStream(os);
			dos.writeUTF(msg);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 向服务器发送修改商品的消息
	 * 
	 * @param Goodsname商品名称
	 * @price price价格
	 * @param num数量
	 * @param intro简介
	 */
	public void changeGoods(String Goodsname, float price, int num, String intro) {
		String msg = "changegoods," + Goodsname + "," + price + "," + num + "," + intro;
		OutputStream os = null;
		DataOutputStream dos = null;
		try {
			os = socket.getOutputStream();
			dos = new DataOutputStream(os);
			dos.writeUTF(msg);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 向服务器发送删除商品的消息
	 * 
	 * @param goodname商品名称
	 */
	public void deleteGoods(String goodname) {
		String msg = "deletegoods," + goodname;
		OutputStream os = null;
		DataOutputStream dos = null;
		try {
			os = socket.getOutputStream();
			dos = new DataOutputStream(os);
			dos.writeUTF(msg);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
